package com.atyuntuku.yuntuku.manager;

import com.atyuntuku.yuntuku.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

/**
 * ClassName: COSManager
 * Package: com.atyuntuku.yuntuku.manager
 * Description:
 *
 * @Author qinglong wei
 * @Create 2025/10/9 21:48
 * @Version 1.0
 */
@Component
public class COSManager {

    @Resource
    CosClientConfig cosClientConfig;

    @Resource
    COSClient cosClient;


    public PutObjectResult putObject(
            String key, File file)
    {
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                cosClientConfig.getBucket(), key, file);
        return cosClient.putObject(putObjectRequest);
    }

    public COSObject getObject(String key){
        GetObjectRequest getObjectRequest = new GetObjectRequest(cosClientConfig.getBucket(), key);
        return cosClient.getObject(getObjectRequest);
    }

    /**
     * 这里是相当于更进一步的上传图片，因为上面只是单纯上传，这里可以进行图片处理，比如这里就返回了一下文件信息
     * @param key
     * @param file
     * @return
     */
    public PutObjectResult putObjectResult(String key,File file)
    {
        //设置上传图片的请求
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        PicOperations picOperations = new PicOperations();
        picOperations.setIsPicInfo(1);//返回文件信息
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }
}
